/*
 * Copyright (C) 2013 AndXmppSDK Project <hljdrl@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.drl.xmpp.storage.table;

import java.util.HashMap;

import org.drl.xmpp.UserInfo;
import org.drl.xmpp.storage.AbstractAccountTable;
import org.drl.xmpp.storage.DataBaseManager;
import org.drl.xmpp.util.StringUtils2;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;

/**
 * 
 * VCard表
 * 
 * @author hljdrl@gmail.com
 * 
 */
public class VCardTable extends AbstractAccountTable {

	private static final class Fields implements BaseColumns{

		private Fields() {
		}

		public static final String KEY_JID = "_JID";
		public static final String KEY_PHOTO_HASH = "_PHOTOHASH";
		public static final String KEY_TIME = "_TIME";

	}

	public static final String KEYS[] = { Fields._ID, Fields.KEY_JID,
			Fields.KEY_PHOTO_HASH, Fields.KEY_TIME };
	public static final String TABLE_NAME = "_VCARDTABLE";

	private final DataBaseManager databaseManager;

	private final static VCardTable instance;

	static {
		instance = new VCardTable(DataBaseManager.getInstance());
	}
	private static HashMap<String, String> avatroCache = new HashMap<String, String>();

	public static HashMap<String, String> loadAvatorCache() {
		return (HashMap<String, String>) java.util.Collections
				.unmodifiableMap(avatroCache);
	}

	public static VCardTable getInstance() {
		return instance;
	}

	public VCardTable(DataBaseManager databaseManager) {
		this.databaseManager = databaseManager;
	}

	@Override
	public void create(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + TABLE_NAME + " (" + Fields._ID
				+ " INTEGER PRIMARY KEY," 
				+ Fields.KEY_JID + " TEXT," + Fields.KEY_TIME + " TEXT,"
				+ Fields.KEY_PHOTO_HASH + " TEXT);";
		DataBaseManager.execSQL(db, sql);

	}

	@Override
	protected String getTableName() {
		return TABLE_NAME;
	}

	@Override
	protected String[] getProjection() {
		return KEYS;
	}
	public void write(String jid,String time,String hash){
		ContentValues cv = new ContentValues();
		String mJid = StringUtils2.parseName(jid);
		cv.put(Fields.KEY_JID,mJid);
		cv.put(Fields.KEY_TIME, time);
		cv.put(Fields.KEY_PHOTO_HASH, hash);
		//
		SQLiteDatabase db = databaseManager.getWritableDatabase();
		Cursor c = db.query(TABLE_NAME, KEYS, Fields.KEY_JID+"=?", new String[]{mJid}, null, null, null);
		if(hasData(c)){
			db.update(TABLE_NAME, cv,  Fields.KEY_JID+"=?", new String[]{mJid});
		}else{
			db.insert(TABLE_NAME, null, cv);
		}
		closeCurosr(c);
		avatroCache.put(StringUtils2.parseName(jid), hash);
		DataBaseManager.notifyTableDataChage(this, true);
	}
	public String finalVCardAvatorImageHash(String jid) {
		String hash = null;
		hash = avatroCache.get(StringUtils2.parseName(jid));
		return hash;
	}

}
